
import Foundation

extension PixelWorldChatWrapper {
    
    //TODO: - 新消息
    @discardableResult
    func newMessages(_ result: PixelWorldChatStatusBlock?) -> Self {
        guard let block = result else { return self }
        self.PixelWorldChat_saveBlock(.newMessage, block: block)
        return self
    }
    
    //TODO: - cmd消息
    @discardableResult
    func cmdMessages(_ result: PixelWorldChatStatusBlock?) -> Self {
        guard let block = result else { return self }
        self.PixelWorldChat_saveBlock(.cmdMessage, block: block)
        return self
    }
    
    //TODO: - 会话列表变化
    @discardableResult
    /// 会话列表变化(全局只保存最后一次订阅)
    /// - Parameter result: 接收回调的block
    /// - Returns:
    func conversationList(_ result: PixelWorldChatStatusBlock?) -> Self {
        guard let block = result else { return self }
        self.PixelWorldChat_saveBlock(.conversation, block: block)
        return self
    }
    
    //TODO: - 未读消息刷新
    @discardableResult
    /// 未读消息刷新(全局只保存最后一次订阅)
    /// - Parameter result: 接收回调的block
    /// - Returns:
    func unreadCount(_ result: PixelWorldChatStatusBlock?) -> Self {
        guard let block = result else { return self }
        self.PixelWorldChat_saveBlock(.unreadCount, block: block)
        return self
    }
    
    //TODO: - 自动登录
    @discardableResult
    /// 自动登录(全局只保存最后一次订阅)
    /// - Parameter result: 接收回调的block
    /// - Returns:
    func autoLogin(_ result: PixelWorldChatStatusBlock?) -> Self {
        guard let block = result else { return self }
        self.PixelWorldChat_saveBlock(.autoLogin, block: block)
        return self
    }
    
    //TODO: - 视频状态
    @discardableResult
    /// 视频状态(全局只保存最后一次订阅)
    /// - Parameter result: 接收回调的block
    /// - Returns:
    func videoStatus(_ result: PixelWorldChatStatusBlock?) -> Self {
        guard let block = result else { return self }
        self.PixelWorldChat_saveBlock(.videoStatus, block: block)
        return self
    }
    
    @discardableResult
    /// 群成员变化通知
    /// - Parameter result: 接收回调的block
    /// - Returns:
    func groupMemberChange(_ result: PixelWorldChatStatusBlock?) -> Self {
        guard let block = result else { return self }
        self.PixelWorldChat_saveBlock(.groupMemberChange, block: block)
        return self
    }
    
    
    //TODO: - 保存所有block
    private func PixelWorldChat_saveBlock(_ type: PixelWorldChatBlockPrefix, block: @escaping PixelWorldChatStatusBlock) {
        guard let baseClass = self.base as? NSObject else { return }
        var baseKey = type.key
        if type == .newMessage || type == .cmdMessage {
            baseKey = type.key + "_" + NSStringFromClass(baseClass.classForCoder)
        }
        var blocks = PixelWorldChatManager.share.ballChat_allMesssageBlocks
        blocks[baseKey] = block
        PixelWorldChatManager.share.ballChat_allMesssageBlocks = blocks
    }
    
    //TODO: - Action
    func responseAction(_ type: PixelWorldChatBlockPrefix, result: PixelWorldChatStatusResult = .success(nil)) {
        DispatchQueue.main.async {
            PixelWorldChatManager.share.ballChat_allMesssageBlocks.filter({ $0.key.contains(type.key) }).forEach { key, block in
                block?(result)
            }
        }
    }
    
}
